

import org.springframework.web.servlet.ModelAndView


import org.authsum.stitches.external.*
import org.authsum.stitches.services.*

class AttributeValuesController {
	
	def stitchesService
	
	def index = { }
	
	def retrieveAttributeValue = { }
	
	
	def removeAttributeValue = {
		
			//
		//get the id and get the contentversion
		
		def attributeValueRequest  = new AttributeValuesRequest()
		//def attributeValueHolder = new AttributeValueHolder()
		
		//if (!attributeValueRequest.attributeValueHolders) {
		//	def attributeValueHolders = nw ArrayOfAttributeValueHolder()
		//	attributeValueRequest.attributeValueHolders = attributeValueHolders
		//}
		
		if (!attributeValueRequest.attributeIds) {
			if (params['attributeId'] ) {
				def attributeIds =new org.authsum.stitches.services.ArrayOfLong()
				
				attributeValueRequest.attributeIds = attributeIds
				attributeIds.long.add(params['attributeId'].toLong()	)			
			}
		}
		if (!attributeValueRequest.attributeValueIds) {
			if (params['attributeValueId'] ) {
				def attributeValueIds = new org.authsum.stitches.services.ArrayOfLong()
				attributeValueRequest.attributeValueIds = attributeValueIds
				attributeValueIds.long.add(params['attributeValueId'].toLong()	)			
			}
		}
		
		
		//def avh = new AttributeValueHolder()
		//bindData(avh,params)
		//need to set the contentVersionId
		bindData(attributeValueRequest,params)
		

		
		//attributeValueRequest.attributeValueHolders.attributeValueHolder.add(avh)
		
		def attributeValueResponse =stitchesService.removeAttributeValues(attributeValueRequest)	
		assert attributeValueResponse.responseCode == '200'
		
		//chain(action:editContentVersion)	
		
		
		
		
	}
	
	
	
	/*
	 * while the stitches api can handle saving multiple attribute values at a time, this simple
	 * UI only does one attribute value at a time
	 */
	def saveAttributeValue = {
		
		def avrequest =new AttributeValuesRequest()
		
		
		
		bindData(avrequest, params)
		
		println avrequest
		
		
		def avh = new AttributeValueHolder()
		bindData(avh,params)
		println avh
		
		if (!avrequest.attributeValueHolders) {
			def attributeValueHolders = new ArrayOfAttributeValueHolder()
			avrequest.attributeValueHolders = attributeValueHolders
			avrequest.attributeValueHolders.attributeValueHolder.add(avh)
		}
		
		
		
		
		
		def avresponse = stitchesService.saveAttributeValues(avrequest)
		println avresponse.responseCode
		assert avresponse.responseCode == "200" 	
		
		
		
	}
	
	def attributeValueBlobForm = {
		
		[ attributename:params['attributename'],contentVersionId:params['contentversionid'].toLong()] 	
		
	}
	
	def upload = {
				

		def uc = new UploadCommand()
		bindData(uc, params)
		if (uc.myFile == null) {
			flash.message = 'file cannot be empty'
			redirect(action:'index')
			
		}else {
			def avrequest = new AttributeValuesRequest()
			
			
			
					bindData(avrequest, params)

			
			def avh = new AttributeValueHolder()
			
			
			avh.name = params['attributeName']
			
			avh.setBinaryData(uc.myFile)
			
			
			
			if (!avrequest.attributeValueHolders) {
				def attributeValueHolders = new ArrayOfAttributeValueHolder()
				avrequest.attributeValueHolders = attributeValueHolders
			}
			
			
			avrequest.attributeValueHolders.attributeValueHolder.add(avh)
			
			
			def avresponse = stitchesService.saveAttributeValues(avrequest)
			println avresponse.responseCode
			assert avresponse.responseCode == "200" 	
			flash.message = 'file updated'
			
			println "redirecting"
			flash.message = 'attribute updated'
			redirect(controller:'search',action:'index')
			//chain(action:index	)
			
			
		}
		
		
	}
	
	
}
